package me.tonywang.quartz.controller;

import me.tonywang.quartz.QuartzUtils;
import me.tonywang.quartz.entity.Crontask;
import me.tonywang.quartz.service.ICrontaskService;
import org.quartz.JobExecutionContext;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;

/**
 * @className: CrontabTaskController
 * @description:
 * @author: WANGHUI
 * @createDate: 2018/5/15 15:54
 * @version: 1.0
 */
@RestController
@RequestMapping("/crontab")
public class CrontaskController {

    @Autowired
    ICrontaskService crontaskService;

    @RequestMapping("/addJob")
    public Object addJob(@RequestBody Crontask task) throws ParseException, SchedulerException {
        crontaskService.saveJob(task);
        return "XXXXsss";
    }


    @RequestMapping("/triggerTempJob")
    public Object triggerTempJob(@RequestBody Crontask task) throws  SchedulerException {
        crontaskService.triggerTempJob(task, 5);
        return "XXXX";
    }


    @RequestMapping("/updateJob")
    public Object updateJob(@RequestBody Crontask task) throws ParseException, SchedulerException, NoSuchMethodException, ClassNotFoundException {
        crontaskService.updateJob(task);
        return "XXXX";
    }

    @RequestMapping("/deleteJob")
    public Object deleteJob(@RequestBody Crontask task) throws   SchedulerException {
        crontaskService.deleteJob(task.getJobName(), task.getJobGroup());
        return "XXXX";
    }

    @RequestMapping("/deleteJobsInGroup")
    public Object deleteJobsInGroup(@RequestBody Crontask task) throws   SchedulerException {
        crontaskService.deleteJobsInGroup(task.getJobGroup());
        return "XXXX";
    }

    @RequestMapping("/pauseJob")
    public Object pauseJob(@RequestBody Crontask task) throws SchedulerException {
        crontaskService.pauseJob(task.getJobName(), task.getJobGroup());
        return "XXXX";
    }

    @RequestMapping("/pauseJobsInGroup")
    public Object pauseJobsInGroup(@RequestBody Crontask task) throws SchedulerException {
        crontaskService.pauseJobsInGroup(task.getJobGroup());
        return "XXXX";
    }

    @RequestMapping("/resumeJob")
    public Object resumeJob(@RequestBody Crontask task) throws SchedulerException {
        crontaskService.resumeJob(task.getJobName(), task.getJobGroup());
        return "XXXX";
    }

    @RequestMapping("/resumeJobsInGroup")
    public Object resumeJobsInGroup(@RequestBody Crontask task) throws SchedulerException {
        crontaskService.resumeJobsInGroup(task.getJobGroup());
        return "XXXX";
    }

    @RequestMapping("/checkCron")
    public Object validateExpression(@RequestBody Crontask task) throws ParseException {
        QuartzUtils.validateExpression(task.getCronExpression());
        return "XXXX";
    }

    @GetMapping("/getMetaData")
    public Object getMetaData( ) throws SchedulerException {
        return crontaskService.getMetaData();
    }


    @GetMapping("/getCurrentlyExecutingJobs")
    public Object getCurrentlyExecutingJobs( ) throws SchedulerException {
        List<JobExecutionContext> contextList = crontaskService.getCurrentlyExecutingJobs();
        for (JobExecutionContext jobExecutionContext : contextList) {
            System.out.println(jobExecutionContext.getJobDetail().getKey());
        }
        return crontaskService.getCurrentlyExecutingJobs().size();
    }


}
